#include<iostream>
#include <string>
#include <queue>
using namespace std;
typedef struct node{
    int data;
    struct node *lchild;
    struct node *rchild;
}BTNode;
BTNode*CreateBT1(int *pre,int *in,int n);
void Preorder(BTNode *BT);
int main(){
	int n,i=0;
	BTNode *BT1;
	cin>>n;
	int post[n],in[n],a;
	for(i=0;i<n;i++){
		cin>>post[i];
	}
	for(i=0;i<n;i++){
		cin>>in[i];
	}
	BT1=CreateBT1(post,in,n);
	cout<<"Preorder:";
	Preorder(BT1);
	return 0;
	
}
BTNode*CreateBT1(int *post,int *in,int n){
	BTNode *BT;
	int k;
	int *p;
	if(n<=0) return NULL;
	BT=new BTNode;
	BT->data=*(post+n-1);
	for(p=in;p<in+n;p++){
		if(*p==*(post+n-1)) break;
	}
	k=p-in;
	BT->lchild=CreateBT1(post,in,k);
	BT->rchild=CreateBT1(post+k,p+1,n-k-1);
	return BT;
}
void Preorder(BTNode *BT){
	if(BT==NULL) return;
	cout<<" "<<BT->data;
	Preorder(BT->lchild);
	Preorder(BT->rchild);
}

